package com.lottery.im.module.service.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.lottery.im.R;
import com.lottery.im.api.result.entity.ServiceData;
import com.lottery.im.widget.NoScrollGridView;
import java.util.List;

public class ServiceAdapter extends BaseAdapter {

  private Context mContext;
  private LayoutInflater mInflater;//布局装载器对象
  private List<ServiceData> dataList;

  private OnServiceClickListener mOnServiceClickListener;

  public ServiceAdapter(Context context, List<ServiceData> dataList) {
    mContext = context;
    this.dataList = dataList;
    mInflater = LayoutInflater.from(context);
  }

  @Override public int getCount() {
    return dataList.size();
  }

  @Override public Object getItem(int position) {
    return dataList.get(position);
  }

  @Override public long getItemId(int position) {
    return position;
  }

  @Override public View getView(final int position, View convertView, ViewGroup parent) {

    ViewHolder holder;
    if (null == convertView) {
      convertView = mInflater.inflate(R.layout.item_service, null);
      holder = new ViewHolder();
      holder.tvTitle = convertView.findViewById(R.id.tv_title);
      holder.grid = convertView.findViewById(R.id.grid);
      convertView.setTag(holder);
    } else {
      holder = (ViewHolder) convertView.getTag();
    }
    ServiceData data = dataList.get(position);
    holder.tvTitle.setText(data.getTitle());

    ServiceGridAdapter gridAdapter =
        new ServiceGridAdapter(mContext, dataList.get(position).getReturnList());
    gridAdapter.setOnServiceItemClickListener(
        childPosition -> mOnServiceClickListener.onServiceClick(position, childPosition));
    holder.grid.setAdapter(gridAdapter);
    return convertView;
  }

  class ViewHolder {
    TextView tvTitle;
    NoScrollGridView grid;
  }

  public void setOnServiceClickListener(
      OnServiceClickListener onServiceClickListener) {
    mOnServiceClickListener = onServiceClickListener;
  }

  public interface OnServiceClickListener {
    void onServiceClick(int position, int childPosition);
  }
}
